/**
 * 
 */
package com.gmail.blackdog1987.carcare.service.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.gmail.blackdog1987.carcare.dao.GasCardDao;
import com.gmail.blackdog1987.carcare.dao.GasCardPayDao;
import com.gmail.blackdog1987.carcare.dao.GasConfigDao;
import com.gmail.blackdog1987.carcare.dao.ProductDao;
import com.gmail.blackdog1987.carcare.dao.hibernate.jdbc.BaseJdbcDao;
import com.gmail.blackdog1987.carcare.dao.hibernate.jdbc.JdbcUtil;
import com.gmail.blackdog1987.carcare.entity.TGasCard;
import com.gmail.blackdog1987.carcare.entity.TGasCardPay;
import com.gmail.blackdog1987.carcare.entity.TGasConfig;
import com.gmail.blackdog1987.carcare.entity.TProduct;
import com.gmail.blackdog1987.carcare.service.GasService;
import com.gmail.blackdog1987.carcare.util.ErrorCode;
import com.gmail.blackdog1987.carcare.util.ImageUploadUtil;

/**
 * 
 * @author 董国超
 *
 */
@Service("gasServiceImpl")
public class GasServiceImpl implements GasService {

	@Autowired
	private GasCardDao gasCardDao;
	@Autowired
	private GasConfigDao gasConfigDao;
	@Autowired
	private BaseJdbcDao baseJdbcDao;
	@Autowired
	private ProductDao productDao;
	@Autowired
	private GasCardPayDao gasCardPayDao;
	
	@Override
	public Map<String, Object> getCard(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", ErrorCode.NO);
		String code = filter.get("code");
		TGasCard card = gasCardDao.getUniqueBy("code", code);
		result.put("result", card);
		return result;
	}

	@Override
	public Map<String, Object> getConfig(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", ErrorCode.NO);
		List<TGasConfig> configs = JdbcUtil.queryForList(baseJdbcDao, TGasConfig.class, " select * from t_gas_config", new Object[]{});
		result.put("result", configs);
		return result;
	}

	@Override
	public Map<String, Object> doApplyCard(Map<String, String> filter, CommonsMultipartFile pic, CommonsMultipartFile pic2) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", ErrorCode.NO);
		String name = filter.get("name");
		String phone = filter.get("phone");
		String address = filter.get("address");
		String price = filter.get("price");
		String userid = filter.get("userid");
		
		TGasCard card = new TGasCard();
		
		// 上传图片到服务器
		if(pic!=null){
			Map<String, Object> imageUploadResult = ImageUploadUtil.imageUpload(new CommonsMultipartFile[] { pic, pic2 });
			if (!ErrorCode.NO.equals(imageUploadResult.get("code")
					.toString())) {
				result.put("code", imageUploadResult.get("code"));
				result.put("msg", imageUploadResult.get("msg"));
				return result;
			}
			// 创建图片信息路径信息
			if (imageUploadResult.get("relativePath") != null) {
				List<String> relativePathList = (List<String>) imageUploadResult.get("relativePath");
				if (relativePathList.size() > 0) {
					card.setIdcardPic(relativePathList.get(0));
					card.setIdcardPic2(relativePathList.get(1));
				}
			}
		}
		
		card.setPrice(Double.valueOf(price));
		card.setName(name);
		card.setPhone(phone);
		card.setAddress(address);
		card.setUserid(Integer.valueOf(userid));
		card.setCode(generateCode());
		card.setStatus(0);
		card.setCreatetime(new Date());
		Serializable id = gasCardDao.create(card);
		card.setId((Integer) id);
		
		TProduct product = new TProduct();
		product.setCreatetime(new Date());
		product.setStatus(0);
		product.setPrice(card.getPrice());
		product.setSubject("车拉丁");
		product.setTableName("t_gas_card");
		product.setTableId(card.getId());
		product.setUserid(Integer.valueOf(userid));
		product.setBody("加油卡");
		productDao.save(product);
		
		result.put("result", card);
		return result;
	}
	
	@Override
	public Map<String, Object> doChargeCard(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", ErrorCode.NO);
		String cardcode = filter.get("cardcode");
		String price = filter.get("price");
		String userid = filter.get("userid");
		
		TGasCardPay pay = new TGasCardPay();
		pay.setCardcode(cardcode);
		pay.setPrice(Double.valueOf(price));
		pay.setCreatetime(new Date());
		pay.setUserid(Integer.valueOf(userid));
		Serializable id = gasCardPayDao.create(pay);
		pay.setId((Integer) id);
		
		TProduct product = new TProduct();
		product.setCreatetime(new Date());
		product.setPrice(pay.getPrice());
		product.setSubject("车拉丁");
		product.setTableName("t_gas_card_pay");
		product.setTableId(pay.getId());
		product.setUserid(Integer.valueOf(userid));
		product.setStatus(0);
		product.setBody("加油卡");
		productDao.create(product);
		result.put("result", pay);
		return result;
	}
	
	private String generateCode(){
		return String.valueOf(System.currentTimeMillis());
	}
}
